﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VehicleWeaponsMod
{
    //public partial class VehicleWeaponsForm : Form
    public partial class VehicleWeaponsForm : GTA.GTAForm
    {
        public VehicleWeapon[] FakeWeapons;
        public VehicleWeaponsShop Shop;
        uint maxindex = 5;
        uint maxweaponcount = 8;
        string[] WeaponNames = {"No Weapon","Shotgun","MP5","AK47","Rocket Launcher"};
        uint[] WeaponIds = { 0, 10, 13, 14, 18 };
        uint[] WeaponPrices = { 0, 250, 500, 750, 1000 };
        GTA.eModel[] WeaponModels = { GTA.eModel.MODEL_CJ_PROP_KNIFE, GTA.eModel.MODEL_CJ_PROP_SHOTGUN, GTA.eModel.MODEL_CJ_PROP_MP5, GTA.eModel.MODEL_CJ_PROP_AK47, GTA.eModel.MODEL_CJ_PROP_RPG };

        uint frontweaponindex = 0;
        uint frontcount = 0;
        uint backweaponindex = 0;
        uint backcount = 0;
        uint roofweaponindex = 0;
        uint roofcount = 0;

        public VehicleWeaponsForm()
        {
            FakeWeapons = new VehicleWeapon[3];
            for (int i = 0; i < FakeWeapons.Length; i++)
            {
                FakeWeapons[i] = new VehicleWeapon();

                if (i != 0) FakeWeapons[i].AttachPoint = VehicleWeapon.eAttachPoint.Back_Bumper; // delete me, debug
            }
            this.UseMouse = true;
            Log.Debug("VehicleWeaponsForm Constructor!");
            InitializeComponent();
        }

        private void TestForm_Load(object sender, EventArgs e)
        {
            Log.Debug("VehicleWeaponsForm Load!");
        }
        new public void Update()
        {
            uint frontprice = WeaponPrices[frontweaponindex] * frontcount;
            uint backprice = WeaponPrices[backweaponindex] * backcount;
            uint roofprice = WeaponPrices[roofweaponindex] * roofcount;
            uint totalprice = frontprice + backprice + roofprice;

            lbl_frnt_price_value.Text = frontprice.ToString();
            lbl_back_price_value.Text = backprice.ToString();
            lbl_roof_price_value.Text = roofprice.ToString();

            lbl_frnt_count_value.Text = frontcount.ToString();
            lbl_back_count_value.Text = backcount.ToString();
            lbl_roof_count_value.Text = roofcount.ToString();

            lbl_frnt_weapon_value.Text = WeaponNames[frontweaponindex];
            lbl_back_weapon_value.Text = WeaponNames[backweaponindex];
            lbl_roof_weapon_value.Text = WeaponNames[roofweaponindex];

            lbl_price_value.Text = totalprice.ToString();



            GTA.Player pl = new GTA.Player();
            GTA.Vehicle plv = pl.GetPlayerPed().GetVehiclePedIsDriving();
            if (!GTA.Vehicle.Exists(plv))
            {
                Log.Error("VehicleWeaponsForm update weapons no vehicle");
                return;
            }



            FakeWeapons[0].Detach();
            FakeWeapons[1].Detach();
            FakeWeapons[2].Detach();

            if (frontweaponindex > 0)
            {
                VehicleWeapon frontweapon = FakeWeapons[0];
                frontweapon.WeaponType = (GTA.eWeapon)WeaponIds[frontweaponindex];
                frontweapon.WeaponCount = (int)frontcount;
                frontweapon.AttachPoint = VehicleWeapon.eAttachPoint.Front_Bumper;
                frontweapon.WeaponModel = WeaponModels[frontweaponindex];
                frontweapon.AttachToVehicle(plv);
            }
            if (backweaponindex > 0)
            {
                VehicleWeapon backweapon = FakeWeapons[1];
                backweapon.WeaponType = (GTA.eWeapon)WeaponIds[backweaponindex];
                backweapon.WeaponCount = (int)backcount;
                backweapon.AttachPoint = VehicleWeapon.eAttachPoint.Back_Bumper;
                backweapon.WeaponModel = WeaponModels[backweaponindex];
                backweapon.AttachToVehicle(plv);
            }
            if (roofweaponindex > 0)
            {
                VehicleWeapon roofweapon = FakeWeapons[2];
                roofweapon.WeaponType = (GTA.eWeapon)WeaponIds[roofweaponindex];
                roofweapon.WeaponCount = (int)roofcount;
                roofweapon.AttachPoint = VehicleWeapon.eAttachPoint.Roof;
                roofweapon.WeaponModel = WeaponModels[roofweaponindex];
                roofweapon.AttachToVehicle(plv);
            }
        }

        // front
        private void btn_frnt_weapon_up_Click(object sender, EventArgs e)
        {
            if (frontweaponindex < maxindex - 1) frontweaponindex++;
            else frontweaponindex = 0;
            Update();
        }
        private void btn_frnt_weapon_down_Click(object sender, EventArgs e)
        {
            if (frontweaponindex > 0) frontweaponindex--;
            else frontweaponindex = maxindex - 1;
            Update();
        }
        private void btn_frnt_count_up_Click(object sender, EventArgs e)
        {
            if (frontcount < maxweaponcount - 1) frontcount++;
            else frontcount = 0;
            Update();
        }
        private void btn_frnt_count_down_Click(object sender, EventArgs e)
        {
            if (frontcount > 0) frontcount--;
            else frontcount = maxweaponcount - 1;
            Update();
        }

        // back
        private void btn_back_weapon_up_Click(object sender, EventArgs e)
        {
            if (backweaponindex < maxindex - 1) backweaponindex++;
            else backweaponindex = 0;
            Update();
        }
        private void btn_back_weapon_down_Click(object sender, EventArgs e)
        {
            if (backweaponindex > 0) backweaponindex--;
            else backweaponindex = maxindex - 1;
            Update();
        }
        private void btn_back_count_up_Click(object sender, EventArgs e)
        {
            if (backcount < maxweaponcount - 1) backcount++;
            else backcount = 0;
            Update();
        }
        private void btn_back_count_down_Click(object sender, EventArgs e)
        {
            if (backcount > 0) backcount--;
            else backcount = maxweaponcount - 1;
            Update();
        }

        // roof
        private void btn_roof_weapon_up_Click(object sender, EventArgs e)
        {
            if (roofweaponindex < maxindex - 1) roofweaponindex++;
            else roofweaponindex = 0;
            Update();
        }
        private void btn_roof_weapon_down_Click(object sender, EventArgs e)
        {
            if (roofweaponindex > 0) roofweaponindex--;
            else roofweaponindex = maxindex - 1;
            Update();
        }
        private void btn_roof_count_up_Click(object sender, EventArgs e)
        {
            if (roofcount < maxweaponcount - 1) roofcount++;
            else roofcount = 0;
            Update();
        }
        private void btn_roof_count_down_Click(object sender, EventArgs e)
        {
            if (roofcount > 0) roofcount--;
            else roofcount = maxweaponcount - 1;
            Update();
        }



        // finish
        public void btn_close_Click(object sender, EventArgs e)
        {
            this.Visible = false;

            if (FakeWeapons[0] != null) FakeWeapons[0].Detach();
            if (FakeWeapons[1] != null) FakeWeapons[1].Detach();
            if (FakeWeapons[2] != null) FakeWeapons[2].Detach();

            if (Shop != null) Shop.ExitShop();
            Shop = null;
            GTA.Screen.PrintString("Shop closed.", 1000);
        }

        unsafe public void btn_buy_Click(object sender, EventArgs e)
        {
            uint frontprice = WeaponPrices[frontweaponindex] * frontcount;
            uint backprice = WeaponPrices[backweaponindex] * backcount;
            uint roofprice = WeaponPrices[roofweaponindex] * roofcount;
            uint totalprice = frontprice + backprice + roofprice;

            GTA.Player pl = new GTA.Player();
            GTA.Vehicle plv = pl.GetPlayerPed().GetVehiclePedIsDriving();
            if (!GTA.Vehicle.Exists(plv))
            {
                Log.Error("btn_buy_Click weapons no vehicle");
                GTA.Screen.PrintString("Error: No vehicle for weapon!",3000);
                return;
            }
            //uint money = GTA.ScriptApi.GetCharMoney(pl.GetPlayerPed());
            uint money=0;
            GTA.ScriptApi.StoreScore(pl, &money);
            if (money < totalprice)
            {
                Log.Message("btn_buy_Click weapons not enough money");
                GTA.Screen.PrintString("Not enough money!", 3000);
                return;
            }


            FakeWeapons[0].Detach();
            FakeWeapons[1].Detach();
            FakeWeapons[2].Detach();

            if (frontweaponindex > 0)
            {
                VehicleWeapon frontweapon = VehicleWeaponsScript.Weapons[0];
                frontweapon.WeaponType = (GTA.eWeapon)WeaponIds[frontweaponindex];
                frontweapon.WeaponCount = (int)frontcount;
                frontweapon.AttachPoint = VehicleWeapon.eAttachPoint.Front_Bumper;
                frontweapon.WeaponModel = WeaponModels[frontweaponindex];
                frontweapon.AttachToVehicle(plv);
            }
            if (backweaponindex > 0)
            {
                VehicleWeapon backweapon = VehicleWeaponsScript.Weapons[1];
                backweapon.WeaponType = (GTA.eWeapon)WeaponIds[backweaponindex];
                backweapon.WeaponCount = (int)backcount;
                backweapon.AttachPoint = VehicleWeapon.eAttachPoint.Back_Bumper;
                backweapon.WeaponModel = WeaponModels[backweaponindex];
                backweapon.AttachToVehicle(plv);
            }
            if (roofweaponindex > 0)
            {
                VehicleWeapon roofweapon = VehicleWeaponsScript.Weapons[2];
                roofweapon.WeaponType = (GTA.eWeapon)WeaponIds[roofweaponindex];
                roofweapon.WeaponCount = (int)roofcount;
                roofweapon.AttachPoint = VehicleWeapon.eAttachPoint.Roof;
                roofweapon.WeaponModel = WeaponModels[roofweaponindex];
                roofweapon.AttachToVehicle(plv);
            }

            this.Visible = false;
            if (Shop != null) Shop.ExitShop();
            Shop = null;
            Log.Debug("Weapons bought, total: " + totalprice);
            GTA.Screen.PrintString("Vehicle Weapons bought! Total cost: $"+totalprice,5000);
            //GTA.ScriptApi.SetCharMoney(pl.GetPlayerPed(), money - totalprice);
            GTA.ScriptApi.AddScore(pl, (0 - (int)totalprice));
        }

        public void CleanUp()
        {
            Log.Debug("VehicleWeaponForm.CleanUp()");
            for (int i = 0; i < FakeWeapons.Length; i++)
            {
                if (FakeWeapons[i] == null) continue;
                FakeWeapons[i].Detach();
                FakeWeapons[i] = null;
            }
            if (Shop != null) Shop.CleanUp();
            Shop = null;
            this.Visible = false;

            Log.Debug("VehicleWeaponForm.CleanUp() Done");
        }
    }
}
